package com.zhht.checkcop.log;

import ch.qos.logback.classic.Level;

import com.zhbc.framework.utils.StringUtils;
import com.zhht.checkcop.log.util.Constant;


/**
 * 日志记录对象
 * @author ningquan
 *
 */
public final class Logger{
	
	public void trace(String msg) {
		performWriteLog(Constant.FORMAT,new Object[]{msg},Level.TRACE);		
	}
	public void trace(String msg, Throwable t) {
		msg = formatMsg(msg);
		logger.trace(msg,t);
	}
	public void trace(String format, Object arg1, Object arg2) {
		performWriteLog(format,new Object[]{arg1,arg2},Level.TRACE);				
	}
	public void trace(String format, Object... arguments) {
		performWriteLog(format,arguments,Level.TRACE);
	}
	public void debug(String msg) {
		performWriteLog(Constant.FORMAT,new Object[]{msg},Level.DEBUG);		
	}
	public void debug(String msg, Throwable t) {
		msg = formatMsg(msg);
		logger.debug(msg,t);
	}
	public void debug(String format, Object arg1, Object arg2) {
		performWriteLog(format,new Object[]{arg1,arg2},Level.DEBUG);		
	}
	public void debug(String format, Object... arguments) {
		performWriteLog(format,arguments,Level.DEBUG);
	}
	public void info(String msg) {
		performWriteLog(Constant.FORMAT,new Object[]{msg},Level.INFO);
	}
	public void info(String msg, Throwable t) {
		msg = formatMsg(msg);
		logger.info(msg,t);
	}
	public void info(String format, Object arg1, Object arg2) {
		performWriteLog(format,new Object[]{arg1,arg2},Level.INFO);		
	}
	public void info(String format, Object... arguments) {
		performWriteLog(format,arguments,Level.INFO);
	}
	public void warn(String msg) {
		performWriteLog(Constant.FORMAT,new Object[]{msg},Level.WARN);
	}
	public void warn(String msg, Throwable t) {
		msg = formatMsg(msg);
		logger.warn(msg,t);		
	}
	public void warn(String format, Object arg1, Object arg2) {
		performWriteLog(format,new Object[]{arg1,arg2},Level.WARN);
	}
	public void warn(String format, Object... arguments) {
		performWriteLog(format,arguments,Level.WARN);
	}
	public void error(String msg) {
		performWriteLog(Constant.FORMAT,new Object[]{msg},Level.ERROR);		
	}
	public void error(String msg, Throwable t) {
		msg = formatMsg(msg);
		logger.error(msg,t);		
	}
	public void error(String format, Object arg1, Object arg2) {
		performWriteLog(format,new Object[]{arg1,arg2},Level.ERROR);		
	}
	public void error(String format, Object... arguments) {
		performWriteLog(format,arguments,Level.ERROR);
	}
	private void performWriteLog(String format, Object[] args, Level level) {
		if(StringUtils.isStrTrimNull(format) || args==null){
			return;
		}
		StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
		Object[] arguments=args;
//		if(StringUtils.isNotStrTrimNull(getTraceId())){
//			format=Constant.FORMAT+Constant.MIDDLE_DELIMETER+format;
//			arguments = new Object[arguments.length+1];
//			StackTraceElement element = stackTrace[3];
//			arguments[0]="{"+element.getFileName()+":"+element.getLineNumber()+"}";
//			for(int i=1;i<arguments.length;i++){
//				arguments[i]=args[i-1];
//			}
//		}
		if(level==Level.DEBUG){
			logger.debug(format, arguments);
		}else if(level==Level.INFO){
			logger.info(format, arguments);
		}else if(level==Level.WARN){
			logger.warn(format, arguments);
		}else if(level==Level.ERROR){
			logger.error(format, arguments);
		}
	}
	private String formatMsg(String msg){
		return msg;
	}
//	private String getTraceId(){
//		return ContextParams.getTraceId();
//	}
	private static org.slf4j.Logger logger = null;
//	public static Logger getNativeLogger(Class<?> clazz){
//		org.slf4j.Logger logger = LoggerFactory.getLogger(clazz);
//		Logger nativeLogger = new Logger(logger);
//		LoggerRegistry.getInstance().registLogger(clazz.getName(), nativeLogger);
//		return nativeLogger;
//	}
//	public static Logger getTargetLogger(Class<?> clazz){
//		 Logger contextLogger = LoggerRegistry.getInstance().getContextLogger(clazz.getName());
//		 if(contextLogger == null){
//			 return getNativeLogger(clazz);
//		 }
//		 return contextLogger;
//	}
	private Logger(){}
	private Logger(org.slf4j.Logger logger) {
		Logger.logger=logger;
	}
}
